<template>
  <div class="container">
    <div class="main">
      <div class="pay-title">
        支付总金额 <span class="pay-price">￥ {{ payInfo.price }}</span>
      </div>
      <div class="pay-main">
        <h4>微信支付</h4>
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          class="pay-qrcode"
        >
          <div class="qrcode">
            <!-- 二维码 -->
            <img id="qrcode-stage" :src="codeUrl">
            <p>请使用微信扫一扫</p>
            <p>扫描二维码支付</p>
          </div>
          <div class="pay-example">
            <img src="http://157.122.54.189:9093/images/wx-sweep2.jpg" alt="">
          </div>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import QRCode from 'qrcode'
export default {
  name: 'Pay',
  data () {
    return {
      payInfo: {
        price: 0
      },
      codeUrl: '',
      timerId: null
    }
  },
  watch: {
    // 这里页面一进来利用immediate强制触发一次函数，但是这时候token还拿不到，所以会报错，因此还要再加上一层判断，有了token之后再发送请求拿数据
    '$store.state.userInfo.token': {
      handler () {
        if (this.$store.state.userInfo.token) {
          this.getPayData()
        }
      },
      immediate: true
    }
  },
  destroyed () {
    clearInterval(this.timerId)
  },
  methods: {
    getPayData () {
      this.$axios({
        url: '/airorders/' + this.$route.query.id,
        headers: {
          Authorization: 'Bearer ' + this.$store.state.userInfo.token
        }
      }).then((res) => {
        this.payInfo = res.data
        QRCode.toDataURL(res.data.payInfo.code_url)
          .then((url) => {
            this.codeUrl = url
            this.checkPay()
          })
          .catch((err) => {
            this.$message.error(err)
          })
      })
    },
    // 轮询
    checkPay () {
      this.timerId = setInterval(() => {
        this.$axios({
          method: 'post',
          url: '/airorders/checkpay',
          headers: {
            Authorization: 'Bearer ' + this.$store.state.userInfo.token
          },
          data: {
            id: this.payInfo.id,
            nonce_str: this.payInfo.price,
            out_trade_no: this.payInfo.payInfo.order_no
          }
        }).then((res) => {
          if (res.data.trade_state === 'SUCCESS') {
            this.$message.success('成功付款 0.01 元')
            clearInterval(this.timerId)
          }
        })
      }, 3000)
    }
  }
}
</script>

<style scoped lang="less">
.container{
    background:#f5f5f5;
    padding: 30px 0;

    .main{
        width:1000px;
        margin:0 auto;

        .pay-title{
            text-align: right;
            span{
                font-size:28px;
                color:orangered;
            }
        }

        .pay-main{
            background:#fff;
            margin-top:10px;
            border-top: 5px orange solid;
            padding:30px;

            h4{
                font-size: 28px;
                font-weight: normal;
                margin-bottom: 10px;
            }

            .pay-qrcode{
                padding:0 80px;
            }

            .qrcode{
                border:1px #ddd solid;
                padding:15px;
                height: fit-content;

                #qrcode-stage{
                    width:200px;
                    height:200px;
                    margin-bottom: 10px;
                }

                p{
                    line-height: 2;
                    text-align: center;
                }
            }
        }
    }
}
</style>
